package cz.cacek.ebook;
import j2me.util.ArrayList;
import j2me.util.Iterator;
import j2me.util.List;

import java.io.InputStream;
import java.io.OutputStream;

import javolution.xml.XMLBinding;
import javolution.xml.XMLFormat;
import javolution.xml.XMLObjectReader;
import javolution.xml.XMLObjectWriter;
import javolution.xml.stream.XMLStreamException;

public class LibraryInfo {

	private List books;

	public String toString() {

		StringBuffer sb = new StringBuffer();
		sb.append("library[books:").append(this.getBooks()).append("]");
		return sb.toString();
	}

	public List getBooks() {
		return books != null ? books : new j2me.util.ArrayList();
	}

	public int getBookCount(){
		return this.getBooks().size();
	}

	public BookInfo getBook(int index){
		return (BookInfo)this.getBooks().get(index);
	}

	public void setBooks(List books) {
		this.books = books;
	}


	public static void write(LibraryInfo a,
			OutputStream output) throws XMLStreamException {
		XMLObjectWriter writer = XMLObjectWriter.newInstance(output);
		writer.setBinding(createXmlBinding()); // Optional.
		writer.setIndentation("\t"); // Optional (use tabulation for
		// indentation).
		writer.write(a);
		writer.close();
	}

	public static LibraryInfo read(InputStream input) throws XMLStreamException {
		XMLObjectReader reader = XMLObjectReader.newInstance(input);
		reader.setBinding(createXmlBinding());
		LibraryInfo b = (LibraryInfo) reader.read();
		reader.close();
		return b;
	}

	private static XMLBinding binding = null;

	private static XMLBinding createXmlBinding() {
		if (binding == null) {
			binding = new XMLBinding() {
				protected final XMLFormat BOOK_XML = new XMLFormat(
						BookInfo.class) {

					public void read(InputElement xml, Object obj)
							throws XMLStreamException {
						BookInfo a = (BookInfo) obj;
						a.setName(xml.getAttribute("name", null));
						a.setId(xml.getAttribute("id", null));
					}

					public void write(Object obj, OutputElement xml)
							throws XMLStreamException {
						BookInfo a = (BookInfo) obj;
						xml.setAttribute("id", a.getId());
						xml.setAttribute("name", a.getName());
					}

				};

				protected final XMLFormat LIBRARY_XML = new XMLFormat(
						LibraryInfo.class) {

					public void read(InputElement xml, Object obj)
							throws XMLStreamException {
						LibraryInfo a = (LibraryInfo) obj;
						List bookList = new ArrayList();
						int count = xml.getAttribute("count", 0);
						for (int i = 0; i < count; i++) {
							BookInfo bk = (BookInfo) xml.get("book",
									BookInfo.class);
							bookList.add(bk);
						}
						a.setBooks(bookList);

						// a.setBooks((List)xml.get("books",ArrayList.class));
					}

					public void write(Object obj, OutputElement xml)
							throws XMLStreamException {
						LibraryInfo a = (LibraryInfo) obj;

						List bookList = a.getBooks();
						if (bookList != null) {
							xml.setAttribute("count", bookList.size());
							for (Iterator iterator = bookList.iterator(); iterator
									.hasNext();) {
								Object bk = (Object) iterator.next();
								xml.add(bk);
							}
						}
					}

				};

				public XMLFormat getFormat(Class cls) {
					if (cls.isAssignableFrom(BookInfo.class)) {
						return BOOK_XML;
					}
					if (cls.isAssignableFrom(LibraryInfo.class)) {
						return LIBRARY_XML;
					}
					return super.getFormat(cls);
				}

			};

			binding.setAlias(BookInfo.class, "book");
			binding.setAlias(LibraryInfo.class, "library");
		}
		return binding;
	}
}
